package homework.four;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test04 {

    public static void main(String[] args) {


        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        File file = new File( "data\\test3.txt" );
        ArrayList<Staff> staffArrayList = new ArrayList<>(  );
        Staff staff1 = new Staff( "张三" ,20,3000.0);
        Staff staff2 = new Staff( "李四" ,23,4500.0);
        staffArrayList.add(staff1);
        staffArrayList.add(staff2);
        try {
            fileOutputStream = new FileOutputStream(file,true);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(staffArrayList);

            fileInputStream = new FileInputStream( file);
            objectInputStream = new ObjectInputStream( fileInputStream );
            ArrayList<Staff> staffList = (ArrayList<Staff>)objectInputStream.readObject();
            staffList.forEach( staff -> {
                System.out.println(staff.toString());
            } );
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
        }finally {
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (objectOutputStream != null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (objectInputStream != null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        File files = new File("data\\test3.txt");
        Staff staff = new Staff("Tom", 30, 5000);
        Test04.saveStaffToFile(staff,files);


        System.out.println("===============================================");
        File file1 = new File( "data\\test3.txt" );
        List<Staff> staffList = Test04.readStaffFromFile(file1);
        staffList.forEach( staff3 -> {
            System.out.println(staff3.toString());
        } );
    }

    public static List<Staff> readStaffFromFile(File file){
        List<Staff> staffList = new ArrayList<>();

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;

        try {
            fileReader = new FileReader( file );
            bufferedReader = new BufferedReader( fileReader );
            String readLine = bufferedReader.readLine();
            while (readLine != null){
                String[] split = readLine.split( "/" );
                for (int i = 0; i < split.length-1;) {
                    staffList.add( new Staff( split[i],Integer.valueOf( split[++i] ),Double.valueOf( split[++i] ) ) );
                }
                readLine = bufferedReader.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return staffList;
    }

    public static void saveStaffToFile(Staff staff, File file){
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream( file,true);
            objectOutputStream = new ObjectOutputStream( fileOutputStream );
            objectOutputStream.writeObject( staff );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}